package com.hotel.reservation.controller.admin;

import com.hotel.reservation.po.Manage;
import com.hotel.reservation.po.Message;
import com.hotel.reservation.po.User;
import com.hotel.reservation.service.ManageService;
import com.hotel.reservation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ManageController {
    @Autowired
    private ManageService manageService;

    @Autowired
    private UserService userService;


    @RequestMapping("/toLogin")
    public String toLogin() {
        return "admin/login";
    }

    @RequestMapping("/login")
    public String adminLogin(@ModelAttribute Manage manage, HttpSession httpSession, Model model) {
        List<Manage> ans = manageService.adminLogin(manage);
        System.out.println(ans.get(0).getManageName());
        if (ans.size() <= 0 || ans == null) {
            model.addAttribute("msg", "登录失败");
            return "admin/login";
        }
        httpSession.setAttribute("manage1", ans.get(0));
        return "admin/main";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser(Model model) {
        model.addAttribute("user", new User());
        return "admin/addUser";
    }

    @RequestMapping("/addUser")
    public String addUser(@ModelAttribute User user, Model model) {
        int ans = manageService.userAdd(user);
        if (ans == -1) {
            model.addAttribute("用户已存在，注册失败");
        }
        return "forward:/admin/showUser";
    }

    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(String tel, Model model) {
        System.out.println(tel);
        User user = userService.findOneUser(tel).get(0);
        model.addAttribute("user", user);
        return "admin/updateUser";
    }

    @RequestMapping("/updateUser")
    public String updateUser(@ModelAttribute User user, Model model) {
        userService.updateUser(user);
        return "admin/main";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(String tel) {
        System.out.println(tel);
        userService.deleteUser(tel);
        return "admin/main";
    }

    @RequestMapping("/showUser")
    public String showAllUser(Model model, Integer pageCur) {
        List<User> allUser = manageService.showUser();
        int temp = allUser.size();
        model.addAttribute("totalCount", temp);
        int totalPage = 0;
        if (temp == 0) {
            totalPage = 0;//总页数
        } else {
            //返回大于或者等于指定表达式的最小整数
            totalPage = (int) Math.ceil((double) temp / 10);
        }
        if (pageCur == null) {
            pageCur = 1;
        }
        if ((pageCur - 1) * 10 > temp) {
            pageCur = pageCur - 1;
        }
        //分页查询
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startIndex", (pageCur - 1) * 10);//起始位置
        map.put("perPageSize", 10);//每页10个
        allUser = userService.selectAllUserByPage(map);
        model.addAttribute("allUser", allUser);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("pageCur", pageCur);
        return "admin/showUser";
    }

    @RequestMapping("/exit")
    public String exit(@ModelAttribute Manage manage, HttpSession session) {
        session.invalidate();
        return "admin/login";
    }
    @RequestMapping("/showManager")
    public String showAllManage(Model model, Integer pageCur) {
        List<Manage> allManage = manageService.showManage();
        int temp = allManage.size();
        model.addAttribute("totalCount", temp);
        int totalPage = 0;
        if (temp == 0) {
            totalPage = 0;//总页数
        } else {
            //返回大于或者等于指定表达式的最小整数
            totalPage = (int) Math.ceil((double) temp / 10);
        }
        if (pageCur == null) {
            pageCur = 1;
        }
        if ((pageCur - 1) * 10 > temp) {
            pageCur = pageCur - 1;
        }
        //分页查询
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startIndex", (pageCur - 1) * 10);//起始位置
        map.put("perPageSize", 10);//每页10个
        allManage = manageService.selectAllManageByPage(map);
        model.addAttribute("allManage", allManage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("pageCur", pageCur);
        return "admin/showManager";
    }

    @RequestMapping("/deleteManage")
    public String deleteUser(int id, Model model) {
        System.out.println(id);
        int ans = manageService.deleteManage(id);
        model.addAttribute("msg", "删除成功");
        return "admin/main";
    }

    @RequestMapping("/toAddManager")
    public String toAddManage(Model model) {
        model.addAttribute("manage", new Manage());
        return "admin/addManager";
    }

    @RequestMapping("/addManage")
    public String addAdmin(@ModelAttribute Manage manage) {
        manageService.adminAdd(manage);
        return "forward:/admin/showManager";
    }

    @RequestMapping("/toUpdateManage")
    public String toUpdateManage(Model model,int id){
      List<Manage> ans = manageService.findOneManage(id);
      model.addAttribute("manage",ans.get(0));
      return "admin/updateManage";
    }

    @RequestMapping("/updateManage")
    public String updateManage(@ModelAttribute Manage manage){
        manageService.updateManageById(manage);
        return "admin/main";
    }

    @RequestMapping("/showMessage")
    public String showMessage(Model model, Integer pageCur){
        List<Message>  messages = manageService.showMessage();
        int temp = messages.size();
        model.addAttribute("totalCount", temp);
        int totalPage = 0;
        if (temp == 0) {
            totalPage = 0;//总页数
        } else {
            //返回大于或者等于指定表达式的最小整数
            totalPage = (int) Math.ceil((double) temp / 10);
        }
        if (pageCur == null) {
            pageCur = 1;
        }
        if ((pageCur - 1) * 10 > temp) {
            pageCur = pageCur - 1;
        }
        //分页查询
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("startIndex", (pageCur - 1) * 10);//起始位置
        map.put("perPageSize", 10);//每页10个
        messages = manageService.selectMessageByPage(map);
        model.addAttribute("message", messages);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("pageCur", pageCur);
        return "admin/showMessage";
    }

    @RequestMapping("/toReplyMessage")
    public String toReply(Model model,int messageId){
        List<Message> messages = manageService.findOneMessage(messageId);
        model.addAttribute("message",messages.get(0));
        return "admin/replyMessage";
    }

    @RequestMapping("replyMessage")
    public String replyMessage(@ModelAttribute Message message){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        message.setMessageReplyTime(df.format(new Date()));
        manageService.updateMessage(message);
        return "admin/main";
    }

    @RequestMapping("deleteMessage")
    public String deleteMessage(int messageId){
        System.out.println(messageId+"messageIdInDelete");
        manageService.deleteMessage(messageId);
        return "admin/main";
    }
}
